<?php
namespace app\index\controller;
use think\Db;
class News extends Base{
    public function news_list(){
        $c_id=input('get.c_id');
        $map = [
            ['state', '=', 1]
        ];
        if($c_id>0){
            array_push($map,['c_id','=',$c_id]);
        }
    	$news_banner=Db::name('news_banner')->where('id',1)->find();
        $headquarters_branches=Db::name('headquarters_branches')->order('sort desc')->select();
        $others=Db::name('others')->where('state',1)->order('sort desc')->select();
        $news_cate=Db::name('news_cate')->where('state','=',1)->order('id desc')->select();
        $news=Db::name('news')->where($map)->order('id desc')->limit(12)->select();
        $this->assign('system_set',$this->system_set());
		$this->assign('news_banner',$news_banner);
        $this->assign('tmdurl',$this->request->domain());
        $this->assign('others',$others);
        $this->assign('headquarters_branches',$headquarters_branches);
        $this->assign('news_cate',$news_cate);
        $this->assign('news',$news);
        $this->assign('c_id',$c_id);
        return $this->fetch();
    }

    public function news_page()
    {
        $page=input("param.page");
        $c_id=input('param.c_id');
        $map = [
            ['state', '=', 1]
        ];
        if($c_id>0){
            array_push($map,['c_id','=',$c_id]);
        }
        $data=Db::name('news')->where($map)->order('id desc')->limit(12*($page-1),12)->select();
        foreach ($data as $key => $value) {
            $data[$key]['detail']=mb_substr(strip_tags($data[$key]['detail']),0,50,'utf-8');
            $data[$key]['time']=date('Y.m.d',$data[$key]['time']);
        }
        $count=Db::name('news')->where($map)->count();
        if($data){
            return json(['code' => 200, 'msg' => '成功','page'=>$page,'pages'=>ceil($count/12),'data'=>$data]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据']);
        }
    }

    public function news_detail(){
        $param = input('get.');
        $c_id=input('param.c_id');
        $map = [
            ['state', '=', 1]
        ];
        if($c_id>0){
            array_push($map,['c_id','=',$c_id]);
        }
        $headquarters_branches=Db::name('headquarters_branches')->order('sort desc')->select();
        $others=Db::name('others')->where('state',1)->order('sort desc')->select();
        $news_cate=Db::name('news_cate')->where('state','=',1)->order('id desc')->select();
        $data=Db::name('news')->where('pubtime','<',time())->where('id',$param['id'])->find();
        $news_banner=Db::name('news_banner')->where('id',1)->find();

        $pre=Db::name('news')->where('pubtime','<',time())->where('id','<',$param['id'])->where($map)->order('id desc')->find();
        $next=Db::name('news')->where('pubtime','<',time())->where('id','>',$param['id'])->where($map)->find();

        $this->assign('system_set',$this->system_set());
        $this->assign('tmdurl',$this->request->domain());
        $this->assign('others',$others);
        $this->assign('headquarters_branches',$headquarters_branches);
        $this->assign('news_cate',$news_cate);
        $this->assign('data',$data);
        $this->assign('news_banner',$news_banner);
        $this->assign('pre',$pre);
        $this->assign('next',$next);
        $this->assign('c_id',$c_id);
        return $this->fetch();
    }
}
